package demo.controller;

import demo.service.SimpleFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/file")
public class SimpleFileController {

    SimpleFileService simpleFileService;

    @Autowired
    SimpleFileController(SimpleFileService simpleFileService){
        this.simpleFileService = simpleFileService;
    }

    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public String uploadFile(@RequestParam("file")MultipartFile file){
        return simpleFileService.upload(file);
    }

    @RequestMapping(value = "/download",method = RequestMethod.GET)
    public ResponseEntity downloadFile(@RequestParam("uuid")String uuid){
        try {
            return simpleFileService.download(uuid);
        }catch (IOException e){
            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping(value = "/info",method = RequestMethod.GET)
    public String getFileInfo(@RequestParam("uuid")String uuid){
        return simpleFileService.getFileInfo(uuid);
    }
}
